Ext.ns("Ext.baiyue");

var webPath = "/bookshop-admin";
Ext.baiyue.AdminMain = function(config){
	var buttonWidth = 60;
	var gridWidth   = 150;
	//工具栏
	var adminBar = new Ext.Toolbar({
		items  : [{
			text  : '新增',
			style : 'align:center',
			width : buttonWidth,
			icon  : webPath+'/images/loginbackground.jpg',
			handler: function(){
				adminInsert();
			},
		},'|',{
			text  : '修改',
			style : 'align:center',
			width : buttonWidth,
			icon  : webPath+'/images/loginbackground.jpg',
			handler: function(){
				var  selection= adminInfoGrid.getSelectionModel().getSelection();
				if(selection.length == 0){
					Ext.Msg.alert("温馨提示","没有选择管理员");
					return;
				}else if(selection.length >1){
					Ext.Msg.alert("温馨提示","您的选择多余一行");
					return;
				}
				var record = selection[0].data;
				adminUpdate(record.adminId,record.name,record.email,record.role);
			}
		},'|',{
			text  : '删除',
			style : 'align:center',
			width : buttonWidth,
			icon  : webPath+'/images/loginbackground.jpg',
			handler: function(){
				var  selection= adminInfoGrid.getSelectionModel().getSelection();
				if(selection.length == 0){
					Ext.Msg.alert("温馨提示","没有选择管理员");
					return;
				}else if(selection.length >1){
					Ext.Msg.alert("温馨提示","您的选择多余一行");
					return;
				}
				adminDelete(selection[0].data.adminId);
			}
		}]
	});
	//model
	Ext.define("adminInfo",{
		extend 	: 'Ext.data.Model',
		fields 	: [{
			name 	: "adminId",
			type 	: 'string',
		},{
			name : 'email',
			type : 'string',
		},{
			name : 'name',
			type : 'string',
		},{
			name : 'role',
			type : 'string',
		}]
	});
	//controller
	var adminStore = new Ext.data.Store({
		model   : 'adminInfo',
		pageSize: 1,
		proxy   : {
			type : 'ajax',
			url  : "list.do",
			reader: {
				type : 'json',
				root : "results",
			},
			autoLoad:true,
		}
	});
	//view
	var adminInfoGrid = new Ext.grid.Panel({
		tbar   	:  adminBar,
		store  	:  adminStore,
		header	: false,
        border	: false,
        draggable: false,
        frame	: false,
        resizable: false,
		region : 'center',
		layout : 'fit',
		width  : '100%',
		columns: [{
			header : 'ID',
			width  : gridWidth,
			dataIndex: 'adminId',
		},{
			header : "名称",
			width  : gridWidth,
			dataIndex: 'name',
		},{
			header : "邮件",
			width  : gridWidth,
			dataIndex: 'email',
		},{
			header : "角色",
			width  : gridWidth,
			dataIndex: 'role',
			renderer:function(value,metadata,record){
				if(value == 'Root'){
					return "超级管理员";
				}
				if(value == "BookAdmin"){
					return "图书管理员";
				}
				if(value == "OrderAdmin"){
					return "订单管理员";
				}
				if(value = "Guest"){
					return "游客";
				}
				if(value = "Staff"){
					return "普通员工";
				}
				return value;
			}
		},{
			id     : "operation",
			flex   : 1,           //自动占满剩余空间.
			header : "操作",
			renderer: function(value,metadata,record,rowIndex,colIndex,store){
				var adminId = record.data.adminId;
				var config  = adminId+",\""+record.data.name+"\",\""+record.data.email+"\",\""+record.data.role+"\"";
				
				var deleteHtml =  "<a href='javascript:void(0)' onclick='adminDelete("+adminId+")'>删除</a>";
				var modifyHtml =  "<a href='javascript:void(0)' onclick='adminUpdate("+config +")'>修改</a>";
				return deleteHtml + " | " +modifyHtml;
			}
		}],
		columnLines : true,
		emptyMsg : "没有管理员",
	});

	Ext.create('Ext.Viewport', {
			layout : 'border',
			items : [ adminInfoGrid ]
	});
	
	adminStore.load();

	this.getStore = function(){
		return adminStore;
	}

	return this;
};

function adminDelete(adminId){
	Ext.Msg.confirm("温馨提示",'您确定要删除该管理员吗？',function(btn){
		if(btn == 'yes'){
			adminManage('deleteAdmin.do',{adminId:adminId});
		}
	});
};

function adminUpdate(adminId,name,email,role){
	var updateWin = new Ext.baiyue.AdminUpdateOrSave({
		title   : "修改管理员信息",
		url     : "adminUpdate.do",
		adminId : adminId,
		name    : name,
		email   : email,
		role    : role,
	});
	updateWin.show();
}
function adminInsert(){
	var updateWin = new Ext.baiyue.AdminUpdateOrSave({
		title   : "添加管理员信息",
		url     : "adminInsert.do",
		insert  : true,
	});
	updateWin.show();
}

function adminManage(url,params){
	Ext.Ajax.request({
		url 	: url,
		params	: params,
		success:function(resp){
			var tmp = eval('('+resp.responseText+")");
			if(tmp.success){
				Ext.Msg.alert("温馨提示",tmp.message);
				adminMain.getStore().reload();
			}else{
				Ext.Msg.alert("温馨提示",tmp.message);
			}
		},
		failure:function(resp){
			Ext.Msg.alert("温馨提示","请求失败");
		}
	});
}


